package com.kushe.dao;

import com.kushe.config.Config;
import com.kushe.entity.*;
import com.kushe.util.core.DaoFactory;
import com.kushe.util.core.DataAccess;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;

import java.util.Date;
import java.util.List;

/**
 * Created by WhiteBlue on 15/4/2.
 */
public class DesignDao extends DataAccess<Design> {


    public Design get(int id) {
        return getDao().fetch(Design.class, id);
    }


    public Design addDesign(String name, double price, String content, int user_id, int sort_id, int category_id, String img) {
        try {
            Design design = new Design();
            design.setName(name);
            design.setPrice(price);
            design.setContent(content);
            design.setUser_id(user_id);
            design.setSort_id(sort_id);
            design.setCategory_id(category_id);
            design.setImg(img);
            design.setTime(new Date());
            design.setType(0);
            design.setCount(0);
            getDao().insert(design);
            return design;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void addCount(int design_id) {
        Design design = getDao().fetch(Design.class, design_id);
        design.setCount(design.getCount() + 1);
        getDao().update(design);
    }


    public QueryResult getBySort(int pageNumber, int sort_id) {
        Pager pager = getDao().createPager(pageNumber, Config.PAGE_SIZE);
        List<Design> list = getDao().query(Design.class, Cnd.where("sort_id", "=", sort_id), pager);
        return new QueryResult(list, pager);
    }

    public QueryResult getByCategory(int pageNumber, int category_id) {
        Pager pager = getDao().createPager(pageNumber, Config.PAGE_SIZE);
        List<Design> list = getDao().query(Design.class, Cnd.where("category_id", "=", category_id), pager);
        return new QueryResult(list, pager);
    }


    public QueryResult getByHot(int pageNumber) {
        Pager pager = getDao().createPager(pageNumber, Config.PAGE_SIZE);
        List<Design> list = getDao().query(Design.class, Cnd.orderBy().desc("count"), pager);
        return new QueryResult(list, pager);
    }

    public QueryResult getByNew(int pageNumber) {
        Pager pager = getDao().createPager(pageNumber, Config.PAGE_SIZE);
        List<Design> list = getDao().query(Design.class, Cnd.orderBy().desc("time"), pager);
        return new QueryResult(list, pager);
    }

    public boolean newPost(int design_id, int user_id, int to_user_id, String content) {
        try {
            Post post = new Post();
            post.setUser_id(user_id);
            post.setContent(content);
            post.setDesign_id(design_id);
            post.setTime(new Date());
            getDao().insert(post);
            DaoFactory.getDao(NoticeDao.class).addDesignMessageNotice(design_id, to_user_id);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    public List<Design> getNewForIndex() {
        return getDao().query(Design.class, Cnd.limit().limit(4).desc("time"));
    }


    public List<Design> getHotForIndex() {
        return getDao().query(Design.class, Cnd.limit().limit(4).desc("count"));
    }


    public QueryResult getPostList(int design_id, int pageNumber) {
        Pager pager = getDao().createPager(pageNumber, Config.PAGE_SIZE);
        List<Post> list = getDao().query(Post.class, Cnd.where("design_id", "=", design_id).desc("time"), pager);
        pager.setRecordCount(getDao().count(Post.class, Cnd.where("design_id", "=", design_id)));
        return new QueryResult(list, pager);
    }


    /**
     * 添加新收藏
     *
     * @param design_id 设计id
     * @param user_id   用户id
     */
    public void addCollect(int design_id, int user_id) {
        try {
            if (!isCollected(design_id, user_id)) {
                Link link = new Link();
                link.setDesign_id(design_id);
                link.setUser_id(user_id);
                link.setTime(new Date());
                getDao().insert(link);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除收藏
     *
     * @param design_id 设计id
     * @param user_id   用户id
     */
    public void deleteCollect(int design_id, int user_id) {
        try {
            Link link = getDao().fetch(Link.class, Cnd.where("design_id", "=", design_id).and("user_id", "=", user_id));
            getDao().delete(link);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 判断是否已经收藏
     *
     * @param design_id 设计id
     * @param user_id   用户id
     */
    public boolean isCollected(int design_id, int user_id) {
        if (getDao().count(Link.class, Cnd.where("design_id", "=", design_id).and("user_id", "=", user_id)) != 0) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 通过收藏查找
     *
     * @param pageNumber 页码
     * @param user_id    用户id
     */
    public QueryResult getByCollect(int pageNumber, int user_id) {
        Pager pager = getDao().createPager(pageNumber, Config.PAGE_SIZE);
        List<Link> list = getDao().query(Link.class, Cnd.where("user_id", "=", user_id));
        pager.setRecordCount(getDao().count(Link.class, Cnd.where("user_id", "=", user_id)));
        return new QueryResult(list, pager);
    }


    /**
     * 得到用户信息上收藏的八个设计
     *
     * @param user_id 用户的id
     */
    public List<Link> getForCollectLimit(int user_id) {
        List<Link> list = getDao().query(Link.class, Cnd.where("user_id", "=", user_id).limit(8));
        return list;
    }


    /**
     * 得到用户信息上的八个设计
     *
     * @param user_id 用户的id
     */
    public List<Design> getForUserLimit(int user_id) {
        List<Design> list = getDao().query(Design.class, Cnd.where("user_id", "=", user_id).limit(8));
        return list;
    }


    public QueryResult search(int pageNumber, String content) {
        Sort sort = getDao().fetch(Sort.class, Cnd.where("name", "like", "%" + content + "%"));
        Category category = getDao().fetch(Category.class, Cnd.where("name", "like", "%" + content + "%"));
        Cnd cnd = null;
        if (sort != null) {
            if (category != null) {
                cnd = Cnd.where("name", "like", "%" + content + "%").or("sort_id", "=", sort.getId()).or("category_id", "=", category.getId());
            } else {
                cnd = Cnd.where("name", "like", "%" + content + "%").or("sort_id", "=", sort.getId());
            }
        } else {
            cnd = Cnd.where("name", "like", "%" + content + "%");
        }
        Pager pager = getDao().createPager(pageNumber, Config.PAGE_SIZE);
        List<Design> list = getDao().query(Design.class, cnd.desc("count"),pager);
        return new QueryResult(list, pager);
    }
}
